<?php

/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: table_forum_polloption_image.php 31112 2012-07-17 09:16:21Z zhengqingpeng $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}
class table_threadpic_plugin extends discuz_table
{
	public function __construct() {

		$this->_table = 'forum_threadpic';
		$this->_pk    = 'aid';
		parent::__construct();
	}
	public function fetch_all($tid,$r=false) {
		global $_G;
		if ($tid) {
			$sqlwhere = ' tid='.$tid;
		}else{
			if ($_G['uid']) {
				$sqlwhere = ' uid='.$_G['uid'].' and fid='.$_G['fid'].' and tid=0';
			}else{
				return false;
			}
		}
		$data = DB::fetch_all('SELECT aid,filename,attachment,thumb,remote,attdesc,uid,width FROM %t WHERE '.$sqlwhere.' order by ox asc ', array($this->_table, $uid, $fid), 'aid');
		$list=array();
		if ($data) {
			require_once libfile('function/home');
			require_once libfile('function/attachment');
			foreach ($data as $val){
				$bigimg =array();
				$val['bigimg'] = pic_get($val['attachment'], 'forum', 0, $val['remote']);
				$val['attachment'] = pic_get($val['attachment'], 'forum', $val['thumb'], $val['remote']);
				$val['widthcode'] = ' width="'.($val['width'] > $_G['setting']['imagemaxwidth'] ? $_G['setting']['imagemaxwidth'] : $val['width']).'"';
				$val['attdesc'] = addslashes($val['attdesc']);
				$val['attdesc'] = str_replace("\r\n","<br>",$val['attdesc']);
				$templist[]=$val;
				$list[] = '['.dimplode($val,',').']';
			}
		}
		if ($r) {
			return $templist;
		}
		return $list;
	}
	public function getthreadimage($tid){
		return DB::fetch_first('SELECT aid,filename,attachment,thumb,remote,attdesc,uid,width FROM %t WHERE tid=%d order by ox asc LIMIT 1 ', array($this->_table, $tid));
	}
	public function qupdata($data,$aid){
		return DB::update($this->_table,$data,array('aid'=>$aid));
	}
	public function count_by_tid($tid) {
		//return DB::result_first('SELECT ox FROM %t WHERE tid=%d order by ox desc limit 1', array($this->_table, $tid));
	}
	public function delete_by_tid($tids) {
		return DB::delete($this->_table, DB::field('tid', $tids));
	}
	public function delete($aid) {
		return DB::delete($this->_table, DB::field('aid', $aid));
	}
	public function clear() {
		require_once libfile('function/forum');
		$deltids = array();
		$query = DB::query("SELECT tid, attachment, thumb FROM %t WHERE tid=0 AND dateline<=%d", array($this->_table, TIMESTAMP - 86400));
		while($attach = DB::fetch($query)) {
			dunlink($attach);
			$deltids[] = $attach['tid'];
		}
		if($deltids) {
			$this->delete_by_tid($deltids);
		}
	}
}

?>